/**
 * 弹层插件
 */
;(function ($) {
    'use strict';

    const _CLASS = 'on';

    $.fn.m_popup = function (opt) {
        opt = opt || {};
        const closeCallBack = opt.closeCallBack;

        const disabled = () => {
            //- 阻止触摸滑动事件
            this.on('touchmove', (event) => {
                event.preventDefault();
            });

            //- 阻止滚动事件
            $(document.body).css({'overflow': 'hidden'});
        };
        const enable = () => {
            hide();
            //- 开启滚动滑动
            $(document.body).removeAttr('style');
        };
        const hide = () => {
            this.removeClass(_CLASS);
            // this.removeClass(_CLASS).transitionEnd(() => {
            //     this.hide();
            // });
        };
        const show = () => {
            this.addClass(_CLASS);
            // this.show().addClass(_CLASS);
            //- 绑定关闭事件元素
            const $close = opt.closeBtn ? $(opt.closeBtn) : this;
            $close.m_touch(() => {
                enable();
                if (closeCallBack) closeCallBack.call(this);
                window.history.go(-1);
            });

            //- 禁用滚动、滑动
            disabled();

            //- 按返回键可关闭弹层
            window.history.pushState({title: 'title', url: '#'}, 'title', '#');
            window.addEventListener('popstate', (e) => {
                if (this.hasClass(_CLASS)) {
                    enable();
                    if (closeCallBack) closeCallBack.call(this);
                }
            }, false);
        };
        show();
    };

})($);